package com.avic.service.impl;

import com.avic.common.util.AssertUtil;
import com.avic.common.util.ex.BizErrorCode;
import com.avic.common.util.ex.BizException;
import com.avic.dal.condition.QueryFleetResourceListCondition;
import com.avic.dal.model.FleetResource;
import com.avic.dal.model.vo.FleetResourceListVO;
import com.avic.service.FleetResourceService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: huangsheng
 * @date: 2018/9/4
 */
@Service
@Slf4j
public class FleetResourceServiceImpl extends AbstractServiceImpl implements FleetResourceService {

    /**
     * 查询车队列表，不分页
     *
     * @see FleetResourceService#itemList(QueryFleetResourceListCondition)
     */
    @Override
    public List<FleetResourceListVO> itemList(QueryFleetResourceListCondition queryFleetResourceListCondition) {
        return fleetResourceMapper.itemList(queryFleetResourceListCondition);
    }

    /**
     * 查询所有车队列表
     *
     * @see FleetResourceService#all()
     */
    @Override
    public List<FleetResource> all() {
        return fleetResourceMapper.all();
    }

    /**
     * 创建资源
     *
     * @see FleetResourceService#create(FleetResource)
     */
    @Override
    public int create(FleetResource fleetResource) {

        AssertUtil.isTrue(fleetResourceMapper.queryItemCountByFleetName(fleetResource.getFleetName()) <= 0,
                BizErrorCode.PARAM_ERROR, "车对名称【" + fleetResource.getFleetName() + "】已经存在");
        try {
            fleetResourceMapper.insert(fleetResource);
            log.info("创建车队资源 {}", fleetResource.getId());
            return fleetResource.getId();
        } catch (DuplicateKeyException de) {
            log.warn("并发创建车对资源", de.getMessage());
            throw new BizException(BizErrorCode.PARAM_ERROR, "车对名称【" + fleetResource.getFleetName() + "】已经存在");
        } catch (Exception e) {
            log.error("创建车对资源发生错误 {}", e.getMessage());
            throw new BizException(BizErrorCode.SYSTEM_ERROR, "创建车对资源发生错误");
        }
    }

    /**
     * 修改资源
     *
     * @see FleetResourceService#update(FleetResource)
     */
    @Override
    public void update(FleetResource fleetResource) {
        log.info("修改车对资源 {}", fleetResource.getId());
        FleetResource dbFleetResource = fleetResourceMapper.queryItemById(fleetResource.getId());
        AssertUtil.isTrue(null != dbFleetResource, BizErrorCode.PARAM_ERROR, "不存在此数据");

        if (!StringUtils.equals(fleetResource.getFleetName(), dbFleetResource.getFleetName())) {
            AssertUtil.isTrue(fleetResourceMapper.queryItemCountByFleetName(fleetResource.getFleetName()) <= 0,
                    BizErrorCode.PARAM_ERROR, "车对名称【" + fleetResource.getFleetName() + "】已经存在");
        }
        try {
            fleetResourceMapper.update(fleetResource);
        } catch (DuplicateKeyException de) {
            log.warn("并发修改车对资源", de.getMessage());
            throw new BizException(BizErrorCode.PARAM_ERROR, "车对名称【" + fleetResource.getFleetName() + "】已经存在");
        } catch (Exception e) {
            log.error("修改车对资源发生错误 {}", e.getMessage());
            throw new BizException(BizErrorCode.SYSTEM_ERROR, "修改车对资源发生错误");
        }
    }

    /**
     * 删除资源
     *
     * @see FleetResourceService#delete(String)
     */
    @Override
    public void delete(int id) {
        try {
            log.info("删除车对资源 {}", id);
            fleetResourceMapper.delete(id);
        } catch (Exception e) {
            log.error("删除车对资源发生错误 {}", e.getMessage());
            throw new BizException(BizErrorCode.SYSTEM_ERROR, "删除车辆资源的时候发生错误");
        }
    }
}
